package basic.chapter1;

/**
 * @author Kevin
 * @date 2020/2/20 15:19
 */

// 以下代码来源于【参考1】
class VolatileExample {
    static int x = 0;
    static volatile boolean v = false;
    public static void writer() {
        x = 42;
        v = true;
    }
    public static void reader() {
        if (v == true) {
            // 这里x会是多少呢？
            System.out.println(x);
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread th1 = new Thread(() -> writer());
        Thread th2 = new Thread(() -> reader());
        //启动两个线程
        th1.start();
        th2.start();
        //等待两个线程执行结束
        th1.join();
        th2.join();
    }
}